package com.candy.db.generator.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 用于加载、读取propertie配置文件的工具类
 * Created by yantingjun on 2015/3/6.
 */
public class PropertyUtil {
    private static List<String> propertyList = new ArrayList<String>();
    private static Properties properties = new Properties();

    /**
     * 加载配置文件
     * @param propertyFileName
     */
    private static void loadProperties(String propertyFileName){
        if(!alreadyLoaded(propertyFileName)){
            InputStream in = null;
            try {
                try{
                    in = new FileInputStream(propertyFileName);//先从当前目录读取
                }catch (FileNotFoundException e){
                    in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
                }
                if(in != null){
                    properties.load(in);
                    propertyList.add(propertyFileName);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                IOUtils.closeQuietly(in);
            }
        }
    }

    /**
     * 是否加载过此配置文件
     * @return
     */
    private static boolean alreadyLoaded(String propertyFileName){
        return propertyList.contains(propertyFileName);
    }

    /**
     * 根据配置文件名称和健名获取值
     * @param propertyFileName 配置文件名称
     * @param key 健名
     * @param default_value 默认值，当根据key取到的value为空时返回default_value
     * @return
     */
    public static int getIntValue(String propertyFileName,String key,int default_value){
        loadProperties(propertyFileName);
        return properties.get(key)==null?default_value:new Integer(properties.get(key)+"");
    }
    public static int getIntValue(String propertyFileName,String key){
        loadProperties(propertyFileName);
        if(properties.get(key)==null){
            throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
        }
        return new Integer(properties.get(key)+"");
    }
    public static String getValue(String propertyFileName,String key,String default_value){
        loadProperties(propertyFileName);
        return properties.get(key)==null?default_value:(String)properties.get(key);
    }
    public static String getValue(String propertyFileName,String key){
        loadProperties(propertyFileName);
        if(properties.get(key)==null){
            throw new IllegalArgumentException("Property:"+key+" was not found!");
        }
        return (String)properties.get(key);
    }
    public static boolean getBooleanValue(String propertyFileName,String key,boolean default_value){
        loadProperties(propertyFileName);
        return properties.get(key)==null?default_value:new Boolean(properties.get(key)+"");
    }
    public static boolean getBooleanValue(String propertyFileName,String key){
        loadProperties(propertyFileName);
        if(properties.get(key)==null){
            throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
        }
        return new Boolean(properties.get(key)+"");
    }
}
